<?php

/**
 * Autoload.
 */
class Autoloader
{
    protected static $_autoloadRootPath = "lib";
    protected static $_autoArray = [];

    public static function setAutoArray($auto_array)
    {
        self::$_autoArray = $auto_array;
    }

    public static function loadByNamespace($name)
    {
        if (in_array($name, self::$_autoArray)) {
            $class_path = str_replace('\\', DIRECTORY_SEPARATOR, $name);
            $namespace = substr($name, 0, strpos($name, '\\'));
            if (!empty($namespace)) {
                $class_file = ROOT_PATH . self::$_autoloadRootPath . DIRECTORY_SEPARATOR . $class_path . '.php';
            } else {
                if (self::$_autoloadRootPath) $class_file = self::$_autoloadRootPath . DIRECTORY_SEPARATOR . $class_path . '.php';
                if (empty($class_file) || !is_file($class_file)) $class_file = ROOT_PATH . DIRECTORY_SEPARATOR . self::$_autoloadRootPath . DIRECTORY_SEPARATOR . "$class_path.php";
            }
            if (is_file($class_file)) {
                require_once($class_file);
                if (class_exists($name, false)) return true;
            }
            throw new Exception($name . " 类未找到");
        }
    }
}